<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="解析便签" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="label">
              <a-input v-model="model.label" placeholder="请输入解析便签" :disabled="isEdit"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="样例条码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="udi">
              <a-input v-model="model.udi" placeholder="请输入或扫描样例条码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="条码类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="part">
              <a-radio-group v-model="model.part" name="radioGroup" :default-value="0">
                <a-radio :value="0"> 一物一码 </a-radio>
                <a-radio :value="1"> 一码多物 </a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <div style="color: #999;margin-left: 190px">静态标识（01）-（17）之间的数值</div>
          <a-col :span="24">
            <a-form-model-item
              label="产品静态标识_起始位置"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="staticS"
            >
              <a-input-number
                v-model="model.staticS"
                :disabled="!model.udi"
                placeholder="请输入产品静态标识起始位置"
                style="width: 100%"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="产品静态标识_结束位置"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="staticE"
            >
              <a-input-number
                v-model="model.staticE"
                :disabled="!model.udi"
                placeholder="请输入产品静态标识结束位置"
                style="width: 100%"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col v-if="static" :span="24">
            <a-form-model-item label="产品静态标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :value="static" style="width: 100%" :disabled="!model.udi" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <div style="color: #999;margin-left: 190px">有效期标识（17）-（10）之间的数值</div>
          <a-col :span="24">
            <a-form-model-item label="有效期_起始位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="validityS">
              <a-input-number
                v-model="model.validityS"
                :disabled="!model.udi"
                placeholder="请输入有效期起始位置"
                style="width: 100%"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="有效期_结束位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="validityE">
              <a-input-number
                v-model="model.validityE"
                :disabled="!model.udi"
                placeholder="请输入有效期_结束位置"
                style="width: 100%"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col v-if="validity" :span="24">
            <a-form-model-item label="有效期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :value="validity" style="width: 100%" :disabled="!model.udi" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <div style="color: #999;margin-left: 190px">批次标识（10）-（91|11|21）之间的数值</div>
          <a-col :span="24">
            <a-form-model-item label="批次_起始位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="batchS">
              <a-input-number
                v-model="model.batchS"
                placeholder="请输入批次起始位置"
                :disabled="!model.udi"
                style="width: 100%"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="批次_结束位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="batchE">
              <a-input-number
                v-model="model.batchE"
                placeholder="请输入批次结束位置"
                style="width: 100%"
                :disabled="!model.udi"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col v-if="batch" :span="24">
            <a-form-model-item label="批次" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :value="batch" style="width: 100%" :disabled="!model.udi" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <div style="color: #999;margin-left: 190px">生产日期（11）-（91|21）之间的数值</div>
          <a-col :span="24">
            <a-form-model-item label="生产日期_起始位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createS">
              <a-input-number
                v-model="model.createS"
                :disabled="!model.udi"
                placeholder="请输入生产日期起始位置"
                style="width: 100%"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="生产日期_结束位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createE">
              <a-input-number
                v-model="model.createE"
                :disabled="!model.udi"
                placeholder="请输入生产日期结束位置"
                style="width: 100%"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col v-if="create" :span="24">
            <a-form-model-item label="生产日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :value="create" style="width: 100%" :disabled="!model.udi" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <div style="color: #999;margin-left: 190px">生产厂家序号（91）到结尾</div>
          <a-col :span="24">
            <a-form-model-item
              label="生产厂家内部序号_起始位置"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="numberS"
            >
              <a-input-number
                v-model="model.numberS"
                :disabled="!model.udi"
                placeholder="请输入生产厂家内部序号起始位置"
                style="width: 100%"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="生产厂家内部序号_结束位置"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="numberE"
            >
              <a-input-number
                v-model="model.numberE"
                placeholder="请输入生产厂家内部序号结束位置"
                style="width: 100%"
                :disabled="!model.udi"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col v-if="number" :span="24">
            <a-form-model-item label="生产厂家内部序号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :value="number" style="width: 100%" :disabled="!model.udi" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <div style="color: #999;margin-left: 190px">序号（91）到结尾</div>
          <a-col :span="24">
            <a-form-model-item label="序号_起始位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serialS">
              <a-input-number
                v-model="model.serialS"
                placeholder="请输入序号起始位置"
                style="width: 100%"
                :disabled="!model.udi"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="序号_结束位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serialrE">
              <a-input-number
                v-model="model.serialrE"
                placeholder="请输入序号结束位置"
                style="width: 100%"
                :disabled="!model.udi"
                :max="model.udi ? model.udi.length : 0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col v-if="serialr" :span="24">
            <a-form-model-item label="序号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :value="serialr" style="width: 100%" :disabled="!model.udi" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <a-input v-model="model.state" placeholder="请输入状态"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="company">
              <a-input v-model="model.company" placeholder="请输入所属公司"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehourse">
              <a-input v-model="model.warehourse" placeholder="请输入所属仓库"></a-input>
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'BmsAnalysisForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        udi: '',
        part: 0,
        staticS: 2,
        staticE: 16,
        validityS: 18,
        validityE: 24,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        label: [{ required: true, message: '请输入', trigger: 'blur' }],
        udi: [{ required: true, message: '请输入或扫描', trigger: 'blur' }],
        part: [{ required: true, trigger: 'change' }],
        staticS: [{ required: true, message: '请输入', trigger: 'blur' }],
        staticE: [{ required: true, message: '请输入', trigger: 'blur' }],
        validityS: [{ required: true, message: '请输入', trigger: 'blur' }],
        validityE: [{ required: true, message: '请输入', trigger: 'blur' }],
        batchS: [{ required: true, message: '请输入', trigger: 'blur' }],
        batchE: [{ required: true, message: '请输入', trigger: 'blur' }],
        createS: [
          {
            validator: (rule, value, callback) => {
              if (typeof value != 'number' && typeof this.model.createE != 'number') {
                callback()
              } else if (typeof value == 'number' && typeof this.model.createE == 'number') {
                if (value > this.model.createE) {
                  callback(new Error('起始位置小于结束位置'))
                } else {
                  callback()
                }
              } else if (typeof value == 'number' && typeof this.model.createE != 'number') {
                callback(new Error('请输入生产日期_结束位置'))
              } else {
                callback(new Error('请输入'))
              }
            },
            trigger: 'blur',
          },
        ],
        numberS: [
          {
            validator: (rule, value, callback) => {
              if (typeof value != 'number' && typeof this.model.numberE != 'number') {
                callback()
              } else if (typeof value == 'number' && typeof this.model.numberE == 'number') {
                if (value > this.model.numberE) {
                  callback(new Error('起始位置小于结束位置'))
                } else {
                  callback()
                }
              } else if (typeof value == 'number' && typeof this.model.numberE != 'number') {
                callback(new Error('请输入生产厂家内部序号_结束位置'))
              } else {
                callback(new Error('请输入'))
              }
            },
            trigger: 'blur',
          },
        ],
        serialS: [
          {
            validator: (rule, value, callback) => {
              if (typeof value != 'number' && typeof this.model.serialrE != 'number') {
                callback()
              } else if (typeof value == 'number' && typeof this.model.serialrE == 'number') {
                if (value > this.model.serialrE) {
                  callback(new Error('起始位置小于结束位置'))
                } else {
                  callback()
                }
              } else if (typeof value == 'number' && typeof this.model.serialrE != 'number') {
                callback(new Error('请输入序号_结束位置'))
              } else {
                callback(new Error('请输入'))
              }
            },
            trigger: 'blur',
          },
        ],
      },
      url: {
        add: '/bms/bmsAnalysis/add',
        edit: '/bms/bmsAnalysis/edit',
        queryById: '/bms/bmsAnalysis/queryById',
      },
      isEdit: false,
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    static() {
      let txt = ''
      const { model } = this
      if (model.udi && typeof model.staticS == 'number' && typeof model.staticE == 'number') {
        txt = model.udi.slice(model.staticS, model.staticE)
      }
      return txt
    },
    validity() {
      let txt = ''
      const { model } = this
      if (model.udi && typeof model.validityS == 'number' && typeof model.validityE == 'number') {
        txt = model.udi.slice(model.validityS, model.validityE)
      }
      return txt
    },
    batch() {
      let txt = ''
      const { model } = this
      if (model.udi && typeof model.batchS == 'number' && typeof model.batchE == 'number') {
        txt = model.udi.slice(model.batchS, model.batchE)
      }
      return txt
    },
    create() {
      let txt = ''
      const { model } = this
      if (model.udi && typeof model.createS == 'number' && typeof model.createE == 'number') {
        txt = model.udi.slice(model.createS, model.createE)
      }
      return txt
    },
    number() {
      let txt = ''
      const { model } = this
      if (model.udi && typeof model.numberS == 'number' && typeof model.numberE == 'number') {
        txt = model.udi.slice(model.numberS, model.numberE)
      }
      return txt
    },
    serialr() {
      let txt = ''
      const { model } = this
      if (model.udi && typeof model.serialS == 'number' && typeof model.serialrE == 'number') {
        txt = model.udi.slice(model.serialS, model.serialrE)
      }
      return txt
    },
  },
  created() {
    // 备份 model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault, false)
    },
    edit(record, isEdit = true) {
      record.part = record.part * 1
      if (record.staticS) {
        record.staticS = record.staticS * 1
      }
      if (record.staticE) {
        record.staticE = record.staticE * 1
      }
      if (record.validityS) {
        record.validityS = record.validityS * 1
      }
      if (record.validityE) {
        record.validityE = record.validityE * 1
      }
      if (record.batchS) {
        record.batchS = record.batchS * 1
      }
      if (record.batchE) {
        record.batchE = record.batchE * 1
      }
      if (record.createS) {
        record.createS = record.createS * 1
      }
      if (record.createE) {
        record.createE = record.createE * 1
      }
      if (record.numberS) {
        record.numberS = record.numberS * 1
      }
      if (record.numberE) {
        record.numberE = record.numberE * 1
      }
      if (record.serialS) {
        record.serialS = record.serialS * 1
      }
      if (record.serialrE) {
        record.serialrE = record.serialrE * 1
      }
      this.model = Object.assign({}, record)
      this.isEdit = isEdit
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.ant-input-disabled {
  color: #333;
}
:deep(.ant-radio-group) {
  .ant-radio-wrapper-disabled {
    span {
      color: #333;
    }
  }
  .ant-radio-disabled .ant-radio-inner::after {
    background-color: #333;
  }
}
</style>